<?php get_header(); ?>

<div id="container">
	<div id="content" class="grid_12">
	<?php if (have_posts()) : while (have_posts()) : the_post();?>
   	
   	<h1>
   		<? the_tags('Bridge ID: ','',''); ?>
   		<? edit_post_link('Edit', '<span class="edit-link">', '</span>'); ?>
   	</h1>

	<hr />

	<?php $args = array(
								'order'          => 'ASC',
								'post_type'      => 'attachment',
								'orderby' => 'menu_order',
								'post_parent'    => $post->ID,
								'post_mime_type' => 'image',
								'post_status'    => null,
								'numberposts'    => -1,
								);
								$attachments = get_posts($args); ?>
								
			<?php if ($attachments) { ?>
				
				<?php $i = 0; // start counter to generate rows ?>
	
				<?php // echo images
				foreach ($attachments as $attachment) { ?>
		
					<div class="grid_6 alpha">
						<?php echo wp_get_attachment_link($attachment->ID, array(420,420)); ?>
						
			            <?php $imageCaption = $attachment->post_excerpt; // this is the caption ?>
			            
			            <?php if ($imageCaption) { ?>
							<div class="clearfix caption">
								<?php $imageCaption = $attachment->post_excerpt; // this is the caption ?>
								<?php echo $imageCaption ?>
							</div>

						<?php } ?>

								<?php $filepath = get_attached_file($attachment->ID); // get the path to the file
								// echo $filepath; ?>
								
								<?php $exif = exif_read_data($filepath, 0, true); ?>
								<?php foreach ($exif as $key => $section) { ?>
									<?php foreach ($section as $name => $val) { ?>
										<?php  // echo "$key.$name: $val<br />\n"; ?>
									<?php } ?>
								<?php } ?>
								<div class="creation-time">Camera Date: <?php echo $section['DateTimeOriginal'] ?></div>
					</div>
					<?php $i++; ?>
					
			        <?php if ($i % 2 == 0) { // ?>
						<div class="clear"></div>
						<hr />
					<?php } ?>
		
				<?php } // end foreach ?>
				
		
			<?php } // end if  ?>
			
			<div class="clear"></div>
		
			<?php $postmeta = get_post_custom(); ?>
			
			<div class="grid_12 alpha clearfix">
				
				<div class="grid_3 alpha">
				<?php if ($postmeta['deckverification'][0] != '') { ?>
					<h4>Deck Verification Method</h4>
					<?php echo $postmeta['deckverification'][0]; ?>
				<?php } ?>
				</div>
				<div class="grid_9 omega">
				<?php if($postmeta['decknotes'][0]) { ?>
					<h4>Deck Notes</h4>
					<?php echo $postmeta['decknotes'][0]; ?>
				<?php } ?>
				</div>
				<div class="clear"></div>
				<hr />
				
				<div class="grid_3 alpha">
				<?php if($postmeta['soffitverification'][0]) { ?>
					<h4>Soffit Verification Method</h4>
					<?php echo $postmeta['soffitverification'][0]; ?>
				<?php } ?>
				</div>
				<div class="grid_9 omega">
				<?php if($postmeta['soffitnotes'][0]) { ?>
					<h4>Soffit Notes</h4>
					<?php echo $postmeta['soffitnotes'][0]; ?>
				<?php } ?>
				</div>
				<div class="clear"></div>
				<hr />
				
				<div class="grid_3 alpha">
				<?php if($postmeta['substructureverification'][0]) { ?>
					<h4>Substructure Verification Method</h4>
					<?php echo $postmeta['substructureverification'][0]; ?>
				<?php } ?>
				</div>
				<div class="grid_9 omega">
				<?php if($postmeta['substructurenotes'][0]) { ?>
					<h4>Substructure Notes</h4>
					<?php echo $postmeta['substructurenotes'][0]; ?>
				<?php } ?>
				</div>
				<div class="clear"></div>
				<hr />
	
				<div class="grid_3 alpha">
				<?php if($postmeta['undersideverification'][0]) { ?>
					<h4>Other Images Verification Method</h4>
					<?php echo $postmeta['undersideverification'][0]; ?>
				<?php } ?>
				</div>
				<div class="grid_9 omega">
				<?php if($postmeta['underdecknotes'][0]) { ?>
					<h4>Other Images Notes</h4>
					test<?php echo $postmeta['underdecknotes'][0]; ?>
				<?php } ?>
				</div>
			<div class="clear"></div>
			<hr />

			</div> <!-- end grid_12 -->

			<h2>Additional Notes</h2>
			<? the_content(); ?>
			
			
	<div class="clear"></div>

	<hr />

	<h2>About this Bridge</h2>
	
	<div class="grid_4 alpha">
		<h3>State</h3>
		<?php the_category() ?>
		<? the_tags('<hr /><h3>Bridge ID</h3><ul class="tag-list"><li>','</li><li>','</li></ul>'); ?>
		<hr />
		
		<h3>Date/time</h3>
	
		<h4>Date</h4>
		<?php echo $postmeta['date'][0]; ?>
		<h4>Sanitized Date for API</h4>
		<?php $date_stamp = str_replace(".","",$postmeta['date'][0]); ?>
		<?php echo $date_stamp; ?>
	
		<h4>Previous Day</h4>
		
		<?php 
		$newdate = strtotime ( '-1 day' , strtotime ( $date_stamp ) ) ;
		$newdate = date ( 'Ymj' , $newdate );
		 echo $newdate; 
		 	
		 ?>
		
		
		<hr />
		<h4>Time</h4>	
		<?php echo $postmeta['time'][0]; ?>
		<h4>Sanitized Time for API</h4>	
		<?php $time_stamp  = DATE("Hi", STRTOTIME($postmeta['time'][0])); ?>
		<?php echo $time_stamp; ?>
		
		<h4>Sanitized Time for 48 hour data</h4>	
		
		<?php $hour = DATE("H", STRTOTIME($postmeta['time'][0])); ?>
		<?php echo $hour; ?>
		
	    <h3>Submitted by: </h3>
	    <?php the_author_posts_link('namefl'); ?> on <?php the_time('l, F jS, Y') ?></p>
	</div>	
	
	<div class="grid_4">

		<?php function DMStoDEC($deg,$min,$sec)
		{
			return $deg+((($min*60)+($sec))/3600);
		}	
		// This script assumes register_globals is on ...
			$dec_la = DMStoDEC($postmeta['latdeg'][0],$postmeta['latmin'][0],$postmeta['latsec'][0]);
			$dec_lo = DMStoDEC($postmeta['longdeg'][0],$postmeta['longmin'][0],$postmeta['longsec'][0]);
		?>

		<h3>Location</h3>
		<?php if ($postmeta['latdeg'][0]) { // if user entered degrees  ?>
			<a href="http://maps.google.com/maps?q=<?php echo $dec_la; ?>,-<?php echo $dec_lo; ?>">Google Map</a>
			<hr />
			<h4>Latitude Deg</h4>
			<?php echo $postmeta['latdeg'][0]; ?>,<?php echo $postmeta['latmin'][0]; ?>,<?php echo $postmeta['latsec'][0]; ?> <?php echo $postmeta['lat_dir'][0]; ?>

			<h4>Latitude Decimal</h4>
			<?php echo $dec_la; ?>
			<hr />
			<h4>Longitude Deg</h4>
			<?php echo $postmeta['longdeg'][0]; ?>,<?php echo $postmeta['longmin'][0]; ?>,<?php echo $postmeta['longsec'][0] ?> <?php echo $postmeta['long_dir'][0]; ?>
	
			<h4>Longitude Decimal</h4>
			<?php echo $dec_lo; ?>
			
			<h4>Coordinates</h4>
			<?php echo $dec_la; ?>,<?php echo $dec_lo; ?>
						
		<?php } ?>
		
		<?php if ($postmeta['lat_decimal'][0]) { // if user entered decimal  ?>
			<a href="http://maps.google.com/maps?q=<?php echo $postmeta['lat_decimal'][0]; ?>,<?php echo $postmeta['long_decimal'][0]; ?>">Google Map</a>

			<h4>Latitude </h4>
			<?php echo $postmeta['lat_decimal'][0]; ?>,<?php echo $postmeta['long_decimal'][0]; ?>
			
			<h4>Longitude</h4>
			<?php echo $postmeta['long_decimal'][0];; ?>
			
			<?php 
				$lat_dec = $postmeta['lat_decimal'][0];
				$long_dec = $postmeta['long_decimal'][0] 
				?>
		
		<?php } ?>
		
		<hr />
		
		<?php 
			if ($dec_la) {
				$url = "http://api.wunderground.com/api/c3bf18d1972e950c/geolookup/history_$date_stamp/conditions/q/$dec_la,-$dec_lo.xml";
				$prev_day_url = "http://api.wunderground.com/api/c3bf18d1972e950c/geolookup/history_$newdate/conditions/q/$dec_la,-$dec_lo.xml";
			} else { // use the decimal lat/long submitted
				$url = "http://api.wunderground.com/api/c3bf18d1972e950c/geolookup/history_$date_stamp/conditions/q/$lat_dec,$long_dec.xml";			
				$prev_day_url = "http://api.wunderground.com/api/c3bf18d1972e950c/geolookup/history_$newdate/conditions/q/$lat_dec,$long_dec.xml";			
			}
		?>

	</div>


	<div class="grid_4 omega">
		
		<?php $xml = simplexml_load_file("$url"); ?>
		<?php $prev_day_xml = simplexml_load_file("$prev_day_url"); ?>
		

		<hr />
		
		<h3>Current Conditions (for testing)</h3>
		<h4>City</h4>
		<?php echo $xml->current_observation[0]->display_location[0]->full; ?>
		
		<h4>Nearest Observation</h4>
		<?php echo $xml->current_observation[0]->observation_location[0]->full; ?>

		<h4>Elevation</h4>
		<?php echo $xml->current_observation[0]->observation_location[0]->elevation; ?>
		
		<h4>Temperature</h4>
		<?php echo $xml->current_observation[0]->temperature_string; ?>

		<h4>Wind</h4>
		<?php echo $xml->current_observation[0]->wind_string; ?>

		<h4>Precipitation</h4>
		<?php echo $xml->current_observation[0]->precip_1hr_string; ?>

		<h4>Temperature</h4>
		<?php echo $xml->current_observation[0]->temperature_string; ?>

		<hr />
		<h4>API Data provided by:</h4>
		<a href="<?php echo $url; ?>"><img src="<?php echo $xml->current_observation[0]->image[0]->url;?>"></a>
		
	</div>	

	<?php endwhile; endif;?>
	
	<div class="clear"></div>
	
	<hr />
	<div class="grid_12 alpha">
			<h3>Weather history</h3>
		
	<TABLE>
		<THEAD>
		     <TR>
			     <td>Date / Time</td>
			     <td>Temperature</td>
			     <td>Wind Direction</td>
			     <td>Rain</td>
			     <td>Clouds</td>
		     </TR>
		</THEAD>
		
		<TBODY>
			<?php $i = 0 ?>
			<?php foreach ($prev_day_xml->xpath('//observation') as $observation) { // get the previous day?>
		     <TR>
			     <td><?php echo $observation->date[0]->pretty; ?></td>
			     <td>Temperature: <?php echo $observation->tempi; ?> F</td>
			     <td>Windspeed: <?php echo $observation->wspdi; ?> MPH</td>
			     <td><?php echo $observation->rain; ?> </td>
			     <td><?php echo $observation->conds; ?></td>
		     </TR>
			<?php } // end foreach?>
			<?php foreach ($xml->xpath('//observation') as $observation) { // get the previous day?>
		     <TR>
			     <td><?php echo $observation->date[0]->pretty; ?></td>
			     <td>Temperature: <?php echo $observation->tempi; ?> F</td>
			     <td>Windspeed: <?php echo $observation->wspdi; ?> MPH</td>
			     <td><?php echo $observation->rain; ?> </td>
			     <td><?php echo $observation->conds; ?></td>
		     </TR>
			<?php } // end foreach?>

		</TBODY>
	</TABLE>

			
			
			
			<?php $i = 0 ?>
			<?php foreach ($prev_day_xml->xpath('//observation') as $observation) { // get the previous day?>
				
					<li><?php echo $observation->date[0]->pretty; ?>
						<ul>
							<li>Temperature: <?php echo $observation->tempi; ?> F</li>
							<li>Windspeed: <?php echo $observation->wspdi; ?> MPG</li>
							<li>Wind Direction: <?php echo $observation->wdird; ?></li>
							<li>Rain: <?php echo $observation->rain; ?> </li>
							<li>Clouds: <?php echo $observation->conds; ?> </li>
						</ul>
					</li>
					
			<?php } // end foreach?>
			
			<?php foreach ($xml->xpath('//observation') as $observation) { ?>
				
				<?php if ( $hour > $observation->date[0]->hour ) { ?>
				
					<li><?php echo $observation->date[0]->pretty; ?>
						<ul>
							<li>Temperature: <?php echo $observation->tempi; ?> F</li>
							<li>Windspeed: <?php echo $observation->wspdi; ?> MPG</li>
							<li>Wind Direction: <?php echo $observation->wdird; ?></li>
							<li>Rain: <?php echo $observation->rain; ?> </li>
							<li>Clouds: <?php echo $observation->conds; ?> </li>
						</ul>
					</li>
				
				<?php  } ?>
				
			<?php } // end foreach?>
			</ul>

	</div>
	
	</div><!-- #content -->
</div><!-- #container -->
<?php get_footer(); ?>
